package com.xunyin.enums;

/**
 * @author jthong
 * @Description:控制板地址（最大允许串联32块控制板）
 * @Version 1.0.0
 * @Date 2019/12/26 16:18
 */
public enum CtrDeviceAddrEnum {
    /***
     * 地址1-32
     */
    ADDR_NUM_1(1, "01"),
    ADDR_NUM_2(2, "02"),
    ADDR_NUM_3(3, "03"),
    ADDR_NUM_4(4, "04"),
    ADDR_NUM_5(5, "05"),
    ADDR_NUM_6(6, "06"),
    ADDR_NUM_7(7, "07"),
    ADDR_NUM_8(8, "08"),
    ADDR_NUM_9(9, "09"),
    ADDR_NUM_10(10, "10"),
    ADDR_NUM_11(11, "11"),
    ADDR_NUM_12(12, "12"),
    ADDR_NUM_13(13, "13"),
    ADDR_NUM_14(14, "14"),
    ADDR_NUM_15(15, "15"),
    ADDR_NUM_16(16, "16"),
    ADDR_NUM_17(17, "17"),
    ADDR_NUM_18(18, "18"),
    ADDR_NUM_19(19, "19"),
    ADDR_NUM_20(20, "20"),
    ADDR_NUM_21(21, "21"),
    ADDR_NUM_22(22, "22"),
    ADDR_NUM_23(23, "23"),
    ADDR_NUM_24(24, "24"),
    ADDR_NUM_25(25, "25"),
    ADDR_NUM_26(26, "26"),
    ADDR_NUM_27(27, "27"),
    ADDR_NUM_28(28, "28"),
    ADDR_NUM_29(29, "29"),
    ADDR_NUM_30(30, "30"),
    ADDR_NUM_31(31, "31"),
    ADDR_NUM_32(32, "32");

    private final String name;
    private final int value;

    CtrDeviceAddrEnum(int value, String name) {
        this.name = name;
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public String getName() {
        return name;
    }

    public int getIndex() {
        return value;
    }

    public static CtrDeviceAddrEnum parse(Integer value) {
        if (value == null) {
            return throwParseException(value);
        }
        for (CtrDeviceAddrEnum e : CtrDeviceAddrEnum.values()) {
            if (e.getValue() == value) {
                return e;
            }
        }
        return throwParseException(value);
    }

    private static CtrDeviceAddrEnum throwParseException(Integer value) {
        throw new IllegalArgumentException(value + " can not parse to " + CtrDeviceAddrEnum.class.getName());
    }

    public static String getNameByValue(Integer value) {
        if (value == null) {
            return "";
        }
        for (CtrDeviceAddrEnum e : CtrDeviceAddrEnum.values()) {
            if (e.getValue() == value) {
                return e.getName();
            }
        }
        return "";
    }
}
